var m = new M("passport");
$(function(){
	if($("#mm_verify").data('id') == 'show') {
		$("#mm_verify").css("display","block");
	}
	
	var $mm_regSelect = $("#mm_regSelect");	
	/** 切换注册方式 **/
	var selectReg = function($mm_regSelect,regInput){
		//CLEAR
		$("#mm_mobile_mail").val("");
		$("#mm_mobileMail_tip").html("");
		$("#mm_pwd").val("");
		$("#mm_pwd_tip").html("");
		$("#mm_mobVerify_tip").html("");
		$("#mm_mobVerifyCode").val("");
		$("#mm_verify_code").html("");
		$("#mm_captcha").val("");
		$("#mm_mobile_mail").removeClass("mm_form_error");
		$("#mm_pwd").removeClass("mm_form_error");

		if($mm_regSelect.text()=="手机注册"){     /** 验证邮箱 */
			$("#mm_verify").css("display","block");
			$("#mm_verfy_phone").css("display","block");
			$mm_regSelect.text("邮箱注册");
			$mm_regSelect.prev().text("手机注册")
			regInput.attr("placeholder","手机号");
			$(".mm_bottom").css("bottom","-40px");
		}else if($mm_regSelect.text() == "邮箱注册"){     /** 验证手机号 */
			$("#mm_verfy_phone").hide();
			$mm_regSelect.text("手机注册");
			$mm_regSelect.prev().text("邮箱注册")
			regInput.attr("placeholder","常用邮箱");
			if($("#mm_verify").data('id') != 'show') {
				$("#mm_verify").css("display","none");
			}
			$(".mm_bottom").css("bottom","0px");
		}
		return false;
	}
	/** 验证邮箱 && 手机号码格式 **/
	var regVerify = {
		/** 验证手机 **/	
		verifyMobile:function($input){
			_validateReg = /^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$/;
			_value = $($input).val();
			if(_value != "" && _validateReg.test(_value) ){
				$("#mm_mobileMail_tip").html("");
				$input.removeClass("mm_form_error");
				return true;
			}else{
				$("#mm_mobileMail_tip").html("请输入正确的手机号");
				$input.addClass("mm_form_error");
				return false;
			}
		},
		/** 验证邮箱 **/	
		verifyEmail:function($input){
			_validateReg = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;
			_value = $($input).val();
			if(_value != "" && _validateReg.test(_value)){
				$("#mm_mobileMail_tip").html("");
				$input.removeClass("mm_form_error");
				return true;
			}else{
				$("#mm_mobileMail_tip").html("请输入正确的邮箱格式，如：xx@xx.com");
				$input.addClass("mm_form_error");
				return false;
			}
		},
		verifyPwd:function($input){
			_validateReg = /[a-zA-Z]+/;
			_value = $($input).val();
			if(_value.length < 21 && _validateReg.test(_value) && _value.length > 5){
				$("#mm_pwd_tip").html("");
				$input.removeClass("mm_form_error");
				return true;
			}else{
				$("#mm_pwd_tip").html("密码长度为6-20位，应包含英文字母");
				$input.addClass("mm_form_error");
				return false;
			}
		},
		verifyPhoneCode:function($input){
			_validateReg = /^\d{6}$/;
			_value = $($input).val();
			if(_value.length != "" && _validateReg.test(_value)){
				$("#mm_mobVerify_tip").html("");
				$input.removeClass("mm_form_error");
				return true;
			}else{
				$("#mm_mobVerify_tip").html("验证码格式错误，请核对后再次输入");
				$input.addClass("mm_form_error");
				return false;
			}
		},
		verifyCaptcha:function($input){
			_validateReg = /^\w{5}$/;
			_value = $($input).val();
			if(_validateReg.test(_value)){
				$("#mm_verify_code").html("");
				$input.removeClass("mm_form_error");
				return true;
			}else{
				$("#mm_verify_code").html("请输入正确的验证码");
				$input.addClass("mm_form_error");
				return false;
			}
		},
	}
	var regByEmail = function(){
		if(regVerify.verifyEmail($("#mm_mobile_mail")) && regVerify.verifyPwd($("#mm_password"))){
			if($('#mm_verify').data('id')=="show"){
				regVerify.verifyCaptcha($("#mm_captcha"));
				param = {
					email: $("#mm_mobile_mail").val(),
					pwd: M.crypto.md5($("#mm_password").val()),
					captcha: $("#mm_captcha").val()
				}
			}else{
				param = {
					email: $("#mm_mobile_mail").val(),
					pwd: M.crypto.md5($("#mm_password").val())
				}
			}
			$.ajax({
				url:'/api/reg/newuser',
				type:"POST",
				dataType:"json",
				data:param,
				success:function(data){
					console.log(data);
					if (data['code'] == "0") {
						if($("#mm_phoneRegFlag").val()=="1"){
							window.location.href = "http://exam.mengma.com/index/indexmobile";
						}else{
							window.location.href = "http://passport.mengma.com/go/index.html?email=" + $("#mm_mobile_mail").val();
						}
					} else if (data['code'] == "400002") {  /*验证码不正确*/
						$("#mm_img_captcha").trigger("click");
						$("#mm_verify_code").html("验证码错误");
						$("#mm_captcha").addClass("mm_form_error");
					}  else if (data['code'] == "400007") {  /*邮箱已经被注册*/
						$("#mm_mobileMail_tip").html("");
						$("#mm_img_captcha").trigger("click");
						$("#mm_mobileMail_tip").html("该邮箱已被使用，请尝试<a href=\"http://passport.mengma.com/?continue=http://www.mengma.com/\">登录</a>");
						$("#mm_mobile_mail").addClass("mm_form_error");
					}else if (data['code'] == "400012") {  /*邮箱格式不正确*/
						$("#mm_img_captcha").trigger("click");
						$("#mm_mobileMail_tip").html("请输入正确的邮箱，如XX@XX.com");
						$("#mm_mobile_mail").addClass("mm_form_error");
					} else if (data['code'] == "400014") {
						window.location.reload();
					}
				}
			});
		}
	}
	//图码 先验证 todo
	var regByPhone =  function (){
		if(regVerify.verifyMobile($("#mm_mobile_mail")) 
			&& regVerify.verifyPwd($("#mm_password"))
			&& regVerify.verifyPhoneCode($("#mm_mobVerifyCode"))
		){
			$.ajax({
				url:'/api/reg/newmobileuser',
				type:"POST",
				dataType:"json",
				data:{
					phone_num: $("#mm_mobile_mail").val(),
					phone_code:$("#mm_mobVerifyCode").val(),
					pwd: M.crypto.md5($("#mm_password").val()),
					captcha: $("#mm_captcha").val()
				},
				success:function(data){
					if (data['code'] == "0") {
						if($("#mm_phoneRegFlag").val()=="1"){
							window.location.href = "http://exam.mengma.com/index/indexmobile";
						}else{
							window.location.href = "http://www.mengma.com/volumes?newUser=1";
						}
					} else if (data['code'] == "400001") {     /*手机号不正确*/
						$("#mm_img_captcha").trigger("click");
						$("#mm_mobileMail_tip").html("请输入正确的手机号码");
						$("#mm_mobile_mail").addClass("mm_form_error");
					}else if (data['code'] == "400002") {   /*验证码不正确*/
						$("#mm_img_captcha").trigger("click");
						$("#mm_verify_code").html("验证码错误");
						$("#mm_captcha").addClass("mm_form_error");
					} else if (data['code'] == "400006") {  /*手机号已经被占用*/
						$("#mm_mobileMail_tip").html("");
						$("#mm_img_captcha").trigger("click");
						$("#mm_mobileMail_tip").html("该手机号已被使用，请尝试<a href=\"http://passport.mengma.com/?continue=http://www.mengma.com/\">登录</a>");
						$("#mm_mobile_mail").addClass("mm_form_error");
					}   else if (data['code'] == "400009") {  /*手机验证码发送失败*/
						
						$("#mm_mobileMail_tip").html("短信验证码发送失败");
						$("#mm_mobile_mail").addClass("mm_form_error");
					} else if (data['code'] == "400011") {  /*手机验证码不正确*/
						$("#mm_mobVerify_tip").html("短信验证码不正确");
						$("#mm_mobVerifyCode").addClass("mm_form_error");
					} else if (data['code'] == "400014") {
						window.location.reload();
					}
				}
			});
		}
	}
	
	var userReg = function(){
		if($("#mm_captcha")[0]) {
			//if(regVerify.verifyCaptcha($("#mm_captcha"))){
				if($mm_regSelect.text()=="手机注册"){     /** 验证邮箱 */
					regByEmail();
				}else if($mm_regSelect.text() == "邮箱注册"){     /** 验证手机号 */
					regByPhone();
				}
			//}
		}else{
			if($mm_regSelect.text()=="手机注册"){     /** 验证邮箱 */
				regByEmail();
			}else if($mm_regSelect.text() == "邮箱注册"){     /** 验证手机号 */
				regByPhone();
			}
		}
		
	}
	/** 切换注册方式 **/
	$mm_regSelect.on("click",function(){
		selectReg($mm_regSelect,$('#mm_mobile_mail'));
	});
	
	/*发送短信验证码*/
	$("#mm_getMobVerCode_btn").on("click",function(){
		var that = $(this);
		if(regVerify.verifyMobile($("#mm_mobile_mail"))) {
			$.ajax({
				url:"/api/reg/isExistMobile",
				type:"POST",
				dataType:"json",
				data:{
					phone_num:$("#mm_mobile_mail").val()
				},
				success:function(data){
					if(data.code=="0"){
						$.ajax({
							url:"/api/reg/sendmobilecode",
							type:"POST",
							dataType:"json",
							data:{
								phone_num:$("#mm_mobile_mail").val(),
								csrf_token:$("#csrf_token").val(),
								captcha: $("#mm_captcha").val()
							},
							success:function(data){
								if(data.code=="0"){
									$("#mm_mobVerify_tip").html("短信验证码已发送，请注意查收");
									timer(that);
								}else if(data.code="400002"){
									$("#mm_verify_code").html("请输入正确的验证码");
									$("#mm_captcha").addClass("mm_form_error");
								}else{
									window.location.reload();
								}
							}
						});	
					}else{
						$("#mm_mobileMail_tip").html("手机号已经被注册，请直接<a href=\"http://passport.mengma.com/?continue=http://www.mengma.com/\">登录</a>");
						$("#mm_img_captcha").trigger("click");
					}
				}
			});
		}
	});
 

	/** 验证手机号or邮箱格式 **/
	$("#mm_mobile_mail").on('blur', function() {
		if($(this).val()){
			if($mm_regSelect.text()=="手机注册") {
				regVerify.verifyEmail($(this));
			} else {
				regVerify.verifyMobile($(this));
			}
		}
	}).on("focus",function(){
		$(this).removeClass("mm_form_error");
		$("#mm_mobileMail_tip").html("");
	});

	$("#mm_password").on("focus",function(){
		$(this).removeClass("mm_form_error");
		$("#mm_pwd_tip").html("");
	}).on("blur",function(){
		$(this).val() ? regVerify.verifyPwd($(this)) : "";
	});

	$("#mm_captcha").on("focus",function(){
		$(this).removeClass("mm_form_error");
		$("#mm_verify_code").html("");
	});

	$("#mm_mobVerifyCode").on("focus",function(){
		$(this).removeClass('mm_form_error');
		$("#mm_mobVerify_tip").html("");
	}).on("blur",function(){
		$(this).val() ? regVerify.verifyPhoneCode($(this)) : "";
	});

	$("#mm_btn_reg").click(userReg);
	/** 短信验证码倒计时 **/
	
	function timer(btn){
		var wait = M.cookie.get("waitTime");
		if(wait == null){
			document.cookie = "waitTime=60;domain=.mengma.com;path=/";
			wait = M.cookie.get("waitTime");
		}
		t = setInterval(function(){
			wait = M.cookie.get("waitTime");
			wait--;
			document.cookie = "waitTime="+wait+";domain=.mengma.com;path=/";
			if(wait==0){
				wait=60;
				clearInterval(t);
				document.cookie = "waitTime="+wait+";domain=.mengma.com;path=/";
				btn.html("获取短信验证码");
				btn.css("background-color","#5ac277");
				btn.attr("disabled",false);
				$("#mm_img_captcha").trigger("click");
			}else{
				wait;
				btn.html("重新发送("+wait+")秒");
				btn.css("background-color","#b3b3b3");
				btn.attr("disabled",true);
			}
		},1000);
	}
	window.onload = function(){
		var t;
		wait = M.cookie.get("waitTime");
		if(wait!=null&&wait!="NaN"&&wait!="undefined"){
			if(wait<=0||wait>=60){
				wait=60;
				clearInterval(t);
				document.cookie = "waitTime="+wait+";domain=.mengma.com;path=/";
			}else if(0<wait<60){
				timer($("#mm_getMobVerCode_btn"));
			}
		}else{
			M.cookie.clear("waitTime","domain",".mengma.com");
		}
	}
	/*手机号动态校验*/
	$("#mm_mobile_mail").on("keyup", function(e) {
		if($mm_regSelect.text() == "邮箱注册"){
			var code = e.keyCode || e.which;
			var phone_num = $("#mm_mobile_mail").val();
			if (phone_num.length == 11 && code) { 
				regVerify.verifyMobile($(this));	
			}
			if(phone_num.length > 11){
				$("#mm_mobile_mail").val(phone_num.substr(0,11));	
			}
		}
	});
	$("input").on("keypress", function(e) {
		var code = e.keyCode || e.which;
		if (code == 13) {
			userReg();		
		}
	});	

});